<template>
  <live-pusher
    ref="livePusher"
    id="livePusher"
    autopush
    :url="url"
    waiting-image="https://storage.zego.im/downloads/pause_publish.png"
    mode="RTC"
    max-bitrate="1500"
    min-bitrate="800"
    @statechange="statechange"
    @netstatus="netstatus"
    @error="pusherError"
  ></live-pusher>
</template>

<script>
import { CommonExpress } from "../../utils/wxmini/common"

export default {
  name: "uniZegoPusher",
  props: {
    url: {
      type: String
    },
    streamID: {
      type: String
    }
  },
  data() {
    return {
      livePusherCtx: ""
    }
  },
  mounted() {
    this.livePusherCtx = uni.createLivePusherContext("livePusher", this)
  },
  methods: {
    async stopPublishingStream(streamID) {
      try {
        const stopRes = await CommonExpress.zegoEngine.stopPublishingStream(streamID)
        this.livePusherCtx.stop()
        return stopRes
      } catch (error) {
        console.log("mytag stopPublishingStream error", error)
      }
    },
    // live-pusher 推流组件状态回调
    statechange(e) {
      this.$emit("statechange",e)
      try {
        if (e.detail.code === -1307) {
          alert("网络断连，且经多次重连抢救无效，更多重试请自行重启推流")
        }
        CommonExpress.zegoEngine.updatePlayerState(this.streamID, e)
      } catch (error) {
        console.log("mytag state ", error)
      }
    },
    // live-pusher 推流网络状态通知
    netstatus(e) {
      try {
        CommonExpress.zegoEngine.updatePlayerNetStatus(this.streamID, e)
      } catch (error) {
        console.log("mytag netState ", error)
      }
    },
    // live-pusher 错误
    pusherError(e) {
      console.warn("mytag pusherError", e)
    }
  }
}
</script>

<style scoped>
live-pusher,
live-player {
  width: 45vw;
  height: 45vw;
  max-width: 200px;
  max-height: 200px;
  margin: 5rpx;
}
</style>
